/**
 * @file   GenericPacket.h
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-21
 * @brief  DON generic packet object (payload data is in a raw form)
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef DON_CORE_PACKET_GENERICPACKET_H
#define DON_CORE_PACKET_GENERICPACKET_H

#include <DON/Core/Packet/AbstractPacket.h>

namespace DON
{
namespace Core
{
namespace DataType
{

class Structure;

}

namespace Packet
{

/**
 * @brief The GenericPacket class
 */
class LIBDONQT4CORESHARED_EXPORT GenericPacket : public AbstractPacket
{
public:
    /**
     * @brief Constructor
     */
    GenericPacket();

    /**
     * @brief Constructor
     * @param source Packet's Source address
     * @param destination Packet's Destination address
     * @param packetType Packet type
     * @param sequence Sequence number
     */
    GenericPacket(const quint16 source,
                  const quint16 destination,
                  const PacketType packetType,
                  const quint16 sequence);

    /**
     * @brief Constructor
     * @param source Packet's Source address
     * @param destination Packet's Destination address
     * @param packetType Packet type
     * @param sequence Sequence number
     * @param payloadData Payload data
     */
    GenericPacket(const quint16 source,
                  const quint16 destination,
                  const PacketType packetType,
                  const quint16 sequence,
                  const QByteArray &payloadData);

    /**
     * @brief Copy constructor
     * @param other Object to be copied
     */
    GenericPacket(const GenericPacket &other);

    /**
     * @brief Destructor
     */
    ~GenericPacket();

    /**
     * @brief Assignment operator
     * @param other Object to be copied
     * @return This object
     */
    GenericPacket & operator=(const GenericPacket &other);

    /**
     * @brief Checks if packet is valid
     * @retval true Packet is valid
     * @retval false Packet is not valid
     */
    bool isValid() const;

    /**
     * @brief Checks if this is a GenericPacket instance
     * @retval true This is a GenericPacket instance
     * @retval false This is not a GenericPacket instance
     */
    bool isGenericPacketInstance() const;

    /**
     * @brief Get packet's payload data
     * @return Packet's payload data
     */
    QByteArray getPayloadData() const;

    /**
     * @brief set packet's payload data
     * @param payloadData New payload data
     * @retval true Success
     * @retval false Error
     */
    bool setPayloadData(const QByteArray &payloadData);

    /**
     * @brief Get payload items
     * @param[out] ok Error status output (true --> success, false --> error)
     * @return A structure that contains items extracted from the payload data
     *
     * @note The caller is responsible for deleting the returned structure. Not doing so will lead
     *       to a memory leak!
     */
    const DataType::Structure * getPayloadItems(bool *ok = NULL) const;

    /**
     * @brief Get maximum number of bytes in payload data
     * @return Maximum number of bytes in payload data
     */
    static int getMaxPayloadDataSize();

    /**
     * @brief Create binary representation of the packet
     * @return Binary data
     */
    QByteArray toBinary() const;

private:
    /**
     * @brief Get the packet's payload data items
     * @param[out] ok Error status output (true --> success, false --> error)
     * @return List of payload data items
     */
    QList<QByteArray> getPayloadDataItems(bool *ok = NULL) const;

    class Private;
    Private * const d;
};

}
}
}

#endif // DON_CORE_PACKET_GENERICPACKET_H
